module namespace app = "http://example.org/ns/webapp/gae/datastore";

import module namespace gae = "http://fgeorges.org/ns/gae/tools";

declare namespace web = "http://expath.org/ns/webapp";

declare function app:save-xquery($request as element(web:request), $bodies as item()*)
{
  <web:response status="200" message="Ok">
     <web:body content-type="application/xml" method="xhtml"/>
  </web:response>
  ,
  let $value := xs:string($request/web:param[@name eq 'value']/@value)
  let $doc   := <value>{ $value }</value>
  let $uri   := 'urn:x-example.org:name:xquery'
  return
    <html xmlns="http://www.w3.org/1999/xhtml">
       <head>
          <title>Save value using XQuery</title>
       </head>
       <body>
          <p>Received value: { $value } (going to save it...)</p>
          {
            if ( gae:save($uri, $doc) ) then
              <p>Successful!</p>
            else
              <p>Something went wrong...</p>
          }
       </body>
    </html>
};
